// pages/point/point.js
const api = require("../request/api")
const request = require("../request/request")
const util = require("../utils/util")
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: 1,
    page: 1,
    page_size: 15,
    page_size_category: 30,
    category_list: [],
    point_list: [],
    current_index: 0,
    type: 0,
    total: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init_data()
    this.get_category_list()
    // this.get_point_list()
  },

  go_search() {
    wx.navigateTo({
      url: '/pages/good/search',
    })
  },

  go_signin() {
    wx.navigateTo({
      url: "/pages/signin/signin",
    })
  },

  init_data() {
    this.setData({
      uid: util.getAppValue(App.globalData.uid),
    })
  },

  get_point_list() {
    let params = {
      uid: this.data.uid,
      page: this.data.page,
      pageSize: this.data.page_size,
      type: this.data.type,
      goods_type: "costpoint"
    }
    util.showLoading('加载中')
    request.post(api.goods_list, params).then(res => {
      util.hideLoading()
      this.setData({
        point_list: res,
      })
    })
  },

  get_category_list(index = 0) {
    let params = {
      uid: this.data.uid,
      page: 1,
      pageSize: this.data.page_size_category,
    }
    request.post(api.category_list, params).then(res => {
      // res.reverse()
      let category_all = {
        id: 0,
        visibility: 1,
        name: "全部推荐",
      }
      let category_list = []
      res.unshift(category_all)
      this.setData({
        category_list: res
      })
      let type = res[index].id
      console.log(type);
      this.setData({
        type
      })
      this.get_point_list()
    })
  },

  go_detail(e) {
    let id = e.currentTarget.dataset.id
    if (!id) {
      id = 10
    }
    wx.navigateTo({
      url: '/pages/good/detail?id=' + id + '&entrance=point',
    })
  },

  choosing_category(e) {
    let current_index = e.currentTarget.dataset.index
    this.setData({
      current_index
    })
    let type = e.currentTarget.dataset.cate_id
    this.setData({
      type
    })
    this.get_point_list()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let page_size = this.data.page_size + 15
    this.setData({
      page_size
    })
    this.get_point_list()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})